<?php

class system_models_Permission extends Zend_Db_Table_Abstract {
    protected $_name = "permission";
    protected $_primary = "PermissionID";
   

    public function getAll($StaffIDList){
        $StaffIDList = explode(',', $StaffIDList);
        $query = "SELECT A.*, B.Name as StaffName, C.Name as UnitName FROM $this->_name A";
        $query .= " Inner Join staff B On A.StaffID = B.StaffID";
        $query .= " Inner Join unit C On B.UnitID = C.UnitID";
        $query .= " Where ".$this->getAdapter()->quoteInto("A.StaffID IN (?)", $StaffIDList);

        $query .= " Order by C.UnitID,B.StaffID";
        try {
            $rows = $this->getAdapter()->fetchAll($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $rows;
    }
    public function getPermission($id){

        $query = "SELECT Roles,DefaultSet FROM $this->_name";
        $query .= " Where ".$this->getAdapter()->quoteInto("StaffID =?", $id);
        try {
            $rows = $this->getAdapter()->fetchAll($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $rows;
    }

    public function getPermissionAssign(){

        $query = "SELECT A.StaffID, B.Name, C.UnitID, C.Name As UnitName FROM $this->_name A";
        $query .= " Inner Join staff B On A.StaffID = B.StaffID";
        $query .= " Inner Join unit C On B.UnitID = C.UnitID";
        $query .= " Where Roles ='150003' And DefaultSet=1";
        try {
            $rows = $this->getAdapter()->fetchAll($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $rows;
    }
    
    public function getPermissionHandle(){

        $query = "SELECT A.StaffID, B.Name, C.UnitID, C.Name As UnitName FROM $this->_name A";
        $query .= " Inner Join staff B On A.StaffID = B.StaffID";
        $query .= " Inner Join unit C On B.UnitID = C.UnitID";
        $query .= " Where Roles ='150006'";
        try {
            $rows = $this->getAdapter()->fetchAll($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $rows;
    }
}
